<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // let p = new Promise((resolve, reject) => {
    //   // 异步任务-Ajax/定时器
    //   setTimeout(() => {
    //     resolve('成功')
    //   }, 2000);
    // })
    // let pb = p.then((res) => {
    //   console.log(res)
    //   // return 2000 //给pb里面的then使用
    //   return new Promise((resolve, reject) => { //return一个对象
    //     setTimeout(() => {
    //       resolve('成功了')
    //     }, 2000);
    //   })
    // })
    // pb.then(result => { //得到的就是上面对象的return的结果的值
    //   console.log(result); // 结果就是成功了
    // })
    // 例如
    function myaxios() {
      let pa = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve('失败了')
        }, 2000);
      })
      return pa
    }
    let pa = myaxios()
    let pb = pa.then((res) => {
      return [null, res]
    }).catch((err) => {
      return [err, null]
    })
    async function myfn() {
      const [err, res] = await pb
      if (err) console.error(err)
      else console.log(res)
    }
    myfn()

  </script>
</body>

</html>